package org.example.listener;

import org.antlr.v4.runtime.*;
import org.checkerframework.checker.units.qual.A;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DiagnosticListener extends BaseErrorListener {
    private final List<Diagnostic> diagnostics = new ArrayList<>();

    @Override
    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
        int start = ((Token) offendingSymbol).getStartIndex();
        int stop = ((Token) offendingSymbol).getStopIndex();

        List<String> stack = ((Parser)recognizer).getRuleInvocationStack(); Collections.reverse(stack);
        System.err.println("rule stack: "+stack);
        System.err.println("line "+line+":"+charPositionInLine +",("+start+","+stop+")"+" at "+ offendingSymbol+": "+msg);

        Diagnostic diagnostic = new Diagnostic(
                new Range(new Position(line-1, charPositionInLine),
                new Position(line-1, charPositionInLine+stop-start+1))
                , msg
                , DiagnosticSeverity.Error,
                "schema forge lint");

        diagnostics.add(diagnostic);
    }

    public List<Diagnostic> getDiagnostics() {
        return diagnostics;
    }
}
